package com.aeoj.clientmanagement.schedule;

import com.aeoj.clientmanagement.constant.PkConstant;
import com.aeoj.clientmanagement.domain.userscore.UserScore;
import com.aeoj.clientmanagement.domain.userscore.dto.UserScoreDTO;
import com.aeoj.clientmanagement.domain.userscore.vo.UserScoreVO;
import com.aeoj.clientmanagement.service.UserScoreService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * author: ZSM
 * time: 2025-04-23 13:17
 */
@Component
@Slf4j
public class RankSchedule {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserScoreService userScoreService;

    @Scheduled(cron = "0 0 * * * *")
    public void refreshRanking() {
        log.info("开始更新排行榜");
        List<UserScoreDTO> list = userScoreService.listRanking();

        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        list.forEach(userScoreDTO -> {
           zSetOperations.add(PkConstant.PK_RANKING_REDIS_KEY, userScoreDTO, userScoreDTO.getScore());
        });

        redisTemplate.expire(PkConstant.PK_RANKING_REDIS_KEY, 3600, TimeUnit.SECONDS);
        log.info("完成更新排行榜");
    }
}
